<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\model\Config;

class Shop extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'shop';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime'; 
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = 'delete_dt';    

    // 追加属性
    protected $append = [
        'shop_type_text',
        'shop_status_text',
        'apply_status_text',
        'category_no_text',
        'business_time_no_text',
        'logo_pic_url',
        'business_license_pic_url',
        'business_time_text',
    ];


    public function getShopTypeList()
    {
        return ['1' => '美团', '2' => '饿了么', '3' => '京东'];
    }

    public function getShopStatusList()
    {
        return ['1' => '正常', '0' => '未上线','-1' => '封禁中'];
    }

    public function getApplyStatusList()
    {
        return ['0' => '审核中', '1' => '通过', '3' => '不通过'];
    }

    public function getCategoryNoList()
    {
        // 店铺类型
        return Config::getConfigs('shop_category');
    }

    public function getBusinessTimeNoList()
    {
        // 店铺类型
        return Config::getConfigs('business_time');
    }


    public function getApplyStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shop_status']) ? $data['shop_status'] : '');
        $list = $this->getApplyStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getShopTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shop_type']) ? $data['shop_type'] : '');
        $list = $this->getShopTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getShopStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shop_status']) ? $data['shop_status'] : '');
        $list = $this->getShopStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCategoryNoTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['category_no']) ? $data['category_no'] : '');
        $list = $this->getCategoryNoList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getBusinessTimeNoTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['business_time_no']) ? $data['business_time_no'] : '');
        $list = $this->getBusinessTimeNoList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getLogoPicUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logo_pic']) ? $data['logo_pic'] : '');
        return strpos($value,'://')===false?config('upload_domain').$value:$value;
    }

    public function getBusinessLicensePicUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['business_license_pic']) ? $data['business_license_pic'] : '');
        return config('upload_domain').$value;
    }

    public function getBusinessTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['business_time']) ? $data['business_time'] : '');
        return str_replace("&quot;", '"', $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'have_uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('admin', 'salesman_admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function platform()
    {
        return $this->belongsTo('shopPlatform', 'shop_no', 'shop_no', [], 'LEFT')->setEagerlyType(0);
    }


    public function play()
    {
        return $this->belongsTo('play', 'shop_no', 'shop_no', [], 'INNER')->setEagerlyType(0);
    }
}
